package com.SXJ.web;

import com.SXJ.pojo.User;
import com.SXJ.service.UserService;
import com.SXJ.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("loginServlet");
        //1. 获取请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");


        //2. 调用xxxService.xxx() 处理业务
        //userService.login()登录
        User loginUser = userService.login(new User(null, username, password, null));


        //3.根据login方法返回结果判断登录是否成功
        if(loginUser == null){
            //不成功，跳回登录页面

            req.setAttribute("msg" , "用户名或密码错误");
            req.setAttribute("username" , username);
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req , resp);
        }else{
            //成功，就跳到login_success.html 页面
            System.out.println("正确可以登录");
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req , resp);
        }
    }
//
//    @Override
//    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
//        System.out.println("Hello SXJ ，Servlet被访问了，登录操作");
//    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
    }

}
